// pages/category/category.js
const util = require("../../utils/utils.js");
const api = require("../../config/api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeKey: 0,
    categoryList: [],
    currentCategory: {},
    currentSubCategory: [],
    goodsCount: 0
  },

  getCategoryIndex: async function () {
    wx.showLoading({
      title: '加载中',
    });
    let res1 = await util.request(api.CategoryIndex);
    if (res1.errno == 0) {
      this.setData({
        categoryList: res1.data.categoryList,
        currentCategory: res1.data.currentCategory,
        currentSubCategory: res1.data.currentSubCategory,
      })
    };
    wx.hideLoading({
      success: (res) => { },
    })
    let res2 = await util.request(api.GoodsCountUrl);
    if (res2.errno == 0) {
      this.setData({
        goodsCount: res2.data.goodsCount
      })
    }
  },

  changeItem: async function (e) {
    wx.showLoading({
      title: '加载中',
    });
    let id = e.target.id;
    let res = await util.request(api.CategoryCurrent, {
      id
    });
    //console.log(res);
    if (res.errno == 0) {
      this.setData({
        currentCategory: res.data.currentCategory,
        currentSubCategory: res.data.currentSubCategory,
      })
    };
    wx.hideLoading({
      success: (res) => { },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCategoryIndex()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})